<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="培训方式" prop="trainMethod">
        <el-select v-model="queryParams.trainMethod" placeholder="请选择培训方式" clearable>
          <el-option
            v-for="dict in dict.type.train_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="培训类型" prop="trainType">
        <el-select v-model="queryParams.trainType" placeholder="请选择培训类型" clearable>
          <el-option
            v-for="dict in dict.type.train_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资格类型" prop="qualifiedType">
        <el-select v-model="queryParams.qualifiedType" placeholder="请选择资格类型" clearable>
          <el-option
            v-for="dict in dict.type.qualified_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="提交状态" prop="isSubmit">
        <el-select v-model="queryParams.isSubmit" placeholder="请选择提交状态" clearable>
          <el-option
            v-for="dict in dict.type.submit_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item> -->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['train:plan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['train:plan:edit']"
        >修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleDelete"
          v-hasPermi="['train:plan:edit']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="培训方式" align="center" prop="trainMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.train_method" :value="scope.row.trainMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="培训类型" align="center" prop="trainType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.train_type" :value="scope.row.trainType"/>
        </template>
      </el-table-column>
      <el-table-column label="资格类型" align="center" prop="qualifiedType" />
      <el-table-column label="作业类别" align="center" prop="workKind" />
      <el-table-column label="操作项目" align="center" prop="operationType" />
      
      <el-table-column label="开始日期" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240px">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="primary"
            style="width: 80px;"
            @click="checkInfo(scope.row)"
          >查看详细信息</el-button>
          <el-button
          v-if="scope.row.isPass == 1"
            size="mini"
            type="success"
            style="width: 80px;"
            @click="createClasses(scope.row)"
          >新增培训班</el-button>
          <el-button
          v-if="scope.row.isSubmit == 0"
            size="mini"
            type="warning"
            style="width: 40px;"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['train:plan:edit']"
          >修改</el-button>

          <el-button
          v-if="scope.row.isSubmit == 0"
            size="mini"
            type="danger"
            style="width: 40px;"
            @click="handleDelete(scope.row)"
          >删除</el-button>

          <el-button
          v-if="scope.row.isSubmit == 0"
            size="mini"
            type="success"
            style="width: 40px; margin-top: 10px;"
            @click="handleSubmit(scope.row)"
          >提交</el-button>

          <el-button
          v-if="scope.row.isSubmit == 1 && scope.row.isPass == 0"
          disabled
          key="autoButton"
            size="mini"
            type="text"
          >审批中</el-button>

          <el-button
          v-if="scope.row.isPass == -1"
          disabled
          key="autoButton"
            size="mini"
            type="text"
          >提交未通过</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改培训计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form :inline="true" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="培训方式" prop="trainMethod">
          <el-select v-model="form.trainMethod" placeholder="请选择培训方式">
            <el-option
              v-for="dict in dict.type.train_method"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="培训类型" prop="trainType">
          <el-select v-model="form.trainType" placeholder="请选择培训类型">
            <el-option
              v-for="dict in dict.type.train_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker clearable
            v-model="form.applyTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="资格类型" prop="qualifiedType">
          <el-select v-model="form.qualifiedType" placeholder="请选择资格类型" @change="getDictDateByPid1(form.qualifiedType)">
            <el-option
              v-for="qualified in qualifiedList"
              :key="qualified.value"
              :label="qualified.label"
              :value="qualified.value"
            ></el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item label="作业类别" prop="workKind">
          <el-select v-model="form.workKind" placeholder="请选择作业类别" @change="getDictDateByPid2(form.workKind)">
            <el-option
              v-for="work in workKindList"
              :key="work.value"
              :label="work.label"
              :value="work.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作项目" prop="operationType">
          <el-select v-model="form.operationType" placeholder="请选择操作项目">
            <el-option
              v-for="operation in operationTypeList"
              :key="operation.value"
              :label="operation.label"
              :value="operation.value"
            ></el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item label="开始日期" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择结束日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="培训计划申请" prop="trainplanApplication" label-width="150px">
          <file-upload v-model="form.trainplanApplication"/>
        </el-form-item>
       
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <!-- 查看详细信息 -->
    <el-dialog :title="title" :visible.sync="openCheck" width="1000px" append-to-body>
      <el-form :inline="true" ref="form" :model="form" :rules="rules" label-width="80px">
        
        <el-form-item label="培训方式" prop="trainMethod">
          <el-select v-model="form.trainMethod" placeholder="请选择培训方式">
            <el-option
              v-for="dict in dict.type.train_method"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="培训类型" prop="trainType">
          <el-select v-model="form.trainType" placeholder="请选择培训类型">
            <el-option
              v-for="dict in dict.type.train_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker clearable
            v-model="form.applyTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="资格类型" prop="qualifiedType">
          <el-select v-model="form.qualifiedType" placeholder="请选择资格类型"  @change="getDictDateByPid1(form.qualifiedType)">
            <el-option
              v-for="qualified in qualifiedList"
              :key="qualified.value"
              :label="qualified.label"
              :value="qualified.value"
            ></el-option> 
          </el-select>
        </el-form-item>
        
        <el-form-item label="作业类别" prop="workKind">
          <el-select v-model="form.workKind" placeholder="请选择作业类别" @change="getDictDateByPid2(form.workKind)">
            <el-option
              v-for="work in workKindList"
              :key="work.value"
              :label="work.label"
              :value="work.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作项目" prop="operationType">
          <el-select v-model="form.operationType" placeholder="请选择操作项目">
            <el-option
              v-for="operation in operationTypeList"
              :key="operation.value"
              :label="operation.label"
              :value="operation.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始日期" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择结束日期">
          </el-date-picker>
        </el-form-item>
        

        <el-form-item label="培训计划申请" prop="trainplanApplication" label-width="150px">
          <file-upload v-model="form.trainplanApplication"/>
        </el-form-item>
       
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cencelCheck">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlan, getPlan, delPlan, addPlan, updatePlan, submitPlan, getDictDateByPid} from "@/api/train/plan";

export default {
  name: "Plan",
  dicts: ['qualified_type', 'operation_type', 'train_type', 'train_method', 'submit_type', 'work_kind'],
  data() {
    return {
      qualifiedList: [],
      workKindList: [],
      operationTypeList: [],
      openCheck: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训计划表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trainMethod: null,
        trainType: null,
        qualifiedType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  
  watch: {
   $route(to, from) {
       window.location.reload(); //监测到路由发生跳转时刷新一次页面
   },
},
  methods: {
    // 安全资格类型标签转换
    qualifiedTypeSwap() {
      for(let i = 0; i < this.qualifiedList.length; i++) {
        if(this.qualifiedList[i].value == this.form.qualifiedType) {
          this.form.qualifiedType = this.qualifiedList[i].label;
          break;
        }
      }
    },
    
    // 作业类别标签类型转换
    workkindTypeSwap() {
      for(let i = 0; i < this.workKindList.length; i++) {
        if(this.workKindList[i].value == this.form.workKind) {
          this.form.workKind = this.workKindList[i].label;
          break;
        }
      };
    },
    // 操作类别类型转换
    operationTypeSwap() {
      for(let i = 0; i < this.operationTypeList.length; i++) {
        if(this.operationTypeList[i].value == this.form.operationType) {
          this.form.operationType = this.operationTypeList[i].label;
          break;
        }
      }
    },
    // 安全资格类型标签转换回来
    qualifiedTypeSwapAfter() {
      for(let i = 0; i < this.qualifiedList.length; i++) {
        if(this.qualifiedList[i].label == this.form.qualifiedType) {
          this.form.qualifiedType = this.qualifiedList[i].value;
          break;
        }
      }
    },
    // 作业类别标签类型转换
    workkindTypeSwapAfter() {
      for(let i = 0; i < this.workKindList.length; i++) {
        if(this.workKindList[i].label == this.form.workKind) {
          this.form.workKind = this.workKindList[i].value;
          break;
        }
      };
    },
    // 操作类别类型转换
    operationTypeSwapAfter() {
      for(let i = 0; i < this.operationTypeList.length; i++) {
        if(this.operationTypeList[i].label == this.form.operationType) {
          this.form.operationType = this.operationTypeList[i].value;
          break;
        }
      }
    },
    // 选择作业类别后触发
    getDictDateByPid2(pid) {
      getDictDateByPid(pid).then(response => {
        this.form.operationType = "";
        this.operationTypeList = response.rows;
      })
    },
    // 选择资格类型后触发
    getDictDateByPid1(pid) {
      getDictDateByPid(pid).then(response => {
        this.workKindList = response.rows;
        this.form.workKind = "";
        this.form.operationType = "";
        this.operationTypeList = [];
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getDictDateByPid(0).then(response => {
        this.open = true;
        this.qualifiedList = response.rows
        this.form.workKind = "";
        this.form.operationType = "";
        this.workKindList = [];
        this.operationTypeList = [];
      })
      this.title = "添加培训计划";
    },
    // 创建培训班
    createClasses(row) {
      const ids = row.id;
        window.location.href = "../class/class?trainId=" + ids;
    },
    /* 提交培训计划 */
    handleSubmit(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认提交?').then(function() {
        return submitPlan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("提交成功");
      }).catch(() => {});
    },
    /** 查询培训计划列表 */
    getList() {
      this.loading = true;
      listPlan(this.queryParams).then(response => {
        this.planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        trainMethod: null,
        trainType: null,
        qualifiedType: null,
        operationType: null,
        startTime: null,
        endTime: null,
        applyUnit: null,
        applyPeople: null,
        applyTime: null,
        passId: null,
        isPass: null,
        isDeleted: null,
        createId: null,
        updateId: null,
        createTime: null,
        updateTime: null,
        extend: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length == 0
      this.multiple = selection.length
    },
    
    cencelCheck() {
      this.openCheck = false;
    },
    // 查看详细信息
    checkInfo(row) {
      this.reset();
      const id = row.id || this.ids
      
      getPlan(id).then(response => {
        this.form = response.data;
        getDictDateByPid(0).then(response => {
        this.qualifiedList = response.rows
        this.qualifiedTypeSwap();
        })
        getDictDateByPid(this.form.qualifiedType).then(response => {
        this.workKindList = response.rows;
        this.workkindTypeSwap();
        })

        getDictDateByPid(this.form.workKind).then(response => {
          this.operationTypeList = response.rows;
          this.operationTypeSwap();
        })
        this.openCheck = true;
        this.title = "查看详细信息";
      });
      
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPlan(id).then(res => {
        this.form = res.data;
        getDictDateByPid(0).then(response => {
        this.qualifiedList = response.rows
        this.qualifiedTypeSwap();
      })
        getDictDateByPid(this.form.qualifiedType).then(response => {
        this.workKindList = response.rows;
        this.workkindTypeSwap();
      })

        getDictDateByPid(this.form.workKind).then(response => {
          this.operationTypeList = response.rows;
          this.operationTypeSwap();
        })
        this.open = true;
        this.title = "修改培训计划";
        getDictDateByPid(0).then(response => {
        this.qualifiedList = response.rows
        this.qualifiedTypeSwapAfter();
      })
        getDictDateByPid(this.form.qualifiedType).then(response => {
        this.workKindList = response.rows;
        this.workkindTypeSwapAfter();
      })

        getDictDateByPid(this.form.workKind).then(response => {
          this.operationTypeList = response.rows;
          this.operationTypeSwapAfter();
        })
      });
      
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePlan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除培训计划编号为"' + ids + '"的数据项？').then(function() {
        return delPlan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('train/plan/export', {
        ...this.queryParams
      }, `plan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
